package com.feinno.urcs.group.interop.task;

import com.feinno.rpc.channel.RpcFuture;
import com.feinno.rpc.client.RpcMethodStub;
import com.feinno.urcs.group.api.FXLogicPoolService;
import com.feinno.urcs.group.api.FXUserIndexService;
import com.feinno.urcs.group.interop.entity.FXCatalogIndex;
import com.feinno.urcs.group.interop.entity.FetionModifyMemberCardArgs;
import com.feinno.urcs.group.siptask.task.GroupTaskEvents;
import org.helium.framework.annotations.FieldSetter;
import org.helium.framework.annotations.ServiceSetter;
import org.helium.framework.annotations.TaskImplementation;
import org.helium.framework.rpc.LegacyRpcClient;
import org.helium.framework.task.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Li.Hongbo <lihongbo@feinno.com>
 */

@TaskImplementation(event = GroupTaskEvents.GROUP_FETION_MODIFY_MEMBER_CARD)

public class FXModifyMemberCardTask implements Task<FetionModifyMemberCardArgs> {


	private static final Logger LOGGER = LoggerFactory.getLogger(FXModifyMemberCardTask.class);
	private String rpcMethodName = "UpdatePersonalInfo";

	@FieldSetter("${FX_GROUP_SERVICE_XML}")
	private LegacyRpcClient rpcClient;
	@ServiceSetter
	private FXLogicPoolService groupLogicPool;
	@ServiceSetter
	private FXUserIndexService fxUserIndexService;

	@Override
	public void processTask(FetionModifyMemberCardArgs args) {
		try {
//			int groupPoolId = groupLogicPool.getLogicPool(args.getGroup().getGroupId());
			FXCatalogIndex fxIndex = fxUserIndexService.getFXCatalogIndex(args.getGroup().getOperatorMobileNo());
			args.getGroup().setOperatorUid(fxIndex.getUserId());
			args.getGroup().setOperatorLogicPool(fxIndex.getLogicalPoolId());
//			args.getGroup().setGroupLogicPool(groupPoolId);
			RpcMethodStub stub = rpcClient.getMethodStub(rpcMethodName);
			RpcFuture future = stub.invoke(args);
			//TODO wait ?
			future.addListener(result -> LOGGER.info(String.format("invoke Fetion RPC Method:%s,retCode=%s,Args=%s", rpcMethodName, result.getValue().getReturnCode(), args.toString())));
		} catch (Exception e) {
			LOGGER.error(String.format("process  error,Args=%s", args.toString()), e);
		}
	}
}
